######################################################
# SetupCitizenSurveyData.R
# This script adds distance variables to the citizen survey
# and codes some variables (used for Fig 2)
#
# Log
# Setup for APSR replication 5 September 2023 by Ryan Jablonski
######################################################
library(haven)

X02citizen_baseline <- read_dta("../Original Survey Files/MALAWI_CITIZEN_BASELINE_withids.dta")
schools.lc=read.csv("./output/Schools.forLC.withdistances_updated.csv")


# rename baseline variables so as to match labels in endline
colnames(X02citizen_baseline)[23] <- "mlw02_q11" # q8 - gender
colnames(X02citizen_baseline)[24] <- "mlw02_q12" # q9 - marital status
colnames(X02citizen_baseline)[25] <- "mlw02_q13" # q10 - tribe
colnames(X02citizen_baseline)[26] <- "mlw02_q13_opt1" # chewa
colnames(X02citizen_baseline)[27] <- "mlw02_q13_opt2" # lomwe
colnames(X02citizen_baseline)[28] <- "mlw02_q13_opt3" # ngoni
colnames(X02citizen_baseline)[29] <- "mlw02_q13_opt4" # nyao
colnames(X02citizen_baseline)[30] <- "mlw02_q13_opt5" # tumbuka
colnames(X02citizen_baseline)[31] <- "mlw02_q13_opt6" # other
colnames(X02citizen_baseline)[32] <- "mlw02_q13_opt7" # don't know
colnames(X02citizen_baseline)[33] <- "mlw02_q13_opt8" # decline to answer
colnames(X02citizen_baseline)[34] <- "mlw02_q13_ot" # specify other
colnames(X02citizen_baseline)[35] <- "mlw02_q14" # q11 - how old are you 
colnames(X02citizen_baseline)[36] <- "mlw02_q15" # q12 - list ranges of ages 
colnames(X02citizen_baseline)[37] <- "mlw02_q16" # q13 - how long live in this ward
colnames(X02citizen_baseline)[38] <- "mlw02_q17" # q14 highest year of education 
colnames(X02citizen_baseline)[39] <- "mlw02_q18" # q15 primary income
colnames(X02citizen_baseline)[40] <- "mlw02_q18_1" # q15_1 farm for profit
colnames(X02citizen_baseline)[41] <- "mlw02_q18_2" # q15_2 nature of business
colnames(X02citizen_baseline)[42] <- "mlw02_q18_ot" # q15_ot
colnames(X02citizen_baseline)[43] <- "mlw02_q19" # q16 have any children
colnames(X02citizen_baseline)[44] <- "mlw02_q20" # q17 how many
colnames(X02citizen_baseline)[45] <- "mlw02_q21" # q18 attend school x
colnames(X02citizen_baseline)[46] <- "base_q19" # q19 classrooms? NB. No equivalent in endline
colnames(X02citizen_baseline)[47] <- "mlw02_q22" # q20 teachers
colnames(X02citizen_baseline)[48] <- "mlw02_q23" # q21 learning materials
colnames(X02citizen_baseline)[49] <- "mlw02_q24" # q22 did you vote in 2014
colnames(X02citizen_baseline)[50] <- "mlw02_q25" # q23 aware of what has done for school x
colnames(X02citizen_baseline)[51] <- "mlw02_q25_1" # q23_1 what has he done for other schools?
colnames(X02citizen_baseline)[52] <- "mlw02_q26" # q24 aware he has done for other schools?
colnames(X02citizen_baseline)[53] <- "mlw02_q27" # q25 NGOs done?
colnames(X02citizen_baseline)[54] <- "mlw02_q27_1" # q25_1 What have NGOs done?
colnames(X02citizen_baseline)[55] <- "mlw02_q28" # q26 how effective is your councillor?
colnames(X02citizen_baseline)[56] <- "mlw02_q29" # q27 you think councillors works harder for others?
colnames(X02citizen_baseline)[57] <- "mlw02_q29_1_ot" # q27#_ot_1 please explain
colnames(X02citizen_baseline)[58] <- "mlw02_q30" # q28 how satisfied are you with his work?
colnames(X02citizen_baseline)[59] <- "mlw02_q31" # q29 how often has he visited?
colnames(X02citizen_baseline)[60] <- "mlw02_q33" # q30 if elections today councillor
colnames(X02citizen_baseline)[61] <- "mlw02_q34" # q31 if elections today MP
colnames(X02citizen_baseline)[62] <- "mlw02_q35" # q32 which party?
colnames(X02citizen_baseline)[63] <- "base_q33" # q33 contact you again - nb. no equivalent in endline
colnames(X02citizen_baseline)[64] <- "base_q34" # q34 phone number - nb. no equivlant in endline
colnames(X02citizen_baseline)[65] <- "base_q34_1" # q34_1 airtel
colnames(X02citizen_baseline)[66] <- "base_q34_2" # q34_2 TNM
colnames(X02citizen_baseline)[67] <- "base_q34_sl" # q34_sl airtel/TNM
colnames(X02citizen_baseline)[68] <- "mlw02_q36" # q35 Note down observations
colnames(X02citizen_baseline)[69] <- "mlw02_q37" # q36 certify

citizen=X02citizen_baseline


citizen_survey <- data.frame(citizen$instanceID,  citizen$mlw02_q3_1,
                            citizen$mlw02_q4_1, citizen$mlw02_q4_2, citizen$mlw02_q5, citizen$mlw02_q6, 
                            citizen$mlw02_q11, citizen$mlw02_q12, citizen$mlw02_q13, citizen$mlw02_q13_opt1, 
                            citizen$mlw02_q13_opt2, citizen$mlw02_q13_opt3, citizen$mlw02_q13_opt4, citizen$mlw02_q13_opt5, 
                            citizen$mlw02_q13_opt6, citizen$mlw02_q13_opt7, citizen$mlw02_q13_opt8, citizen$mlw02_q13_ot, 
                            citizen$mlw02_q14, citizen$mlw02_q16, citizen$mlw02_q17, citizen$mlw02_q18, 
                            citizen$mlw02_q18_1, citizen$mlw02_q18_2, citizen$mlw02_q19, citizen$mlw02_q20, 
                            citizen$mlw02_q21, citizen$mlw02_q22, citizen$mlw02_q23, citizen$mlw02_q24, 
                            citizen$mlw02_q25, citizen$mlw02_q25_1, citizen$mlw02_q26, citizen$mlw02_q27, citizen$mlw02_q27_1, 
                            citizen$mlw02_q28, citizen$mlw02_q29, citizen$mlw02_q29_1_ot, citizen$mlw02_q30, 
                            citizen$mlw02_q31, citizen$mlw02_q33, citizen$mlw02_q34, citizen$mlw02_q35, citizen$base_q34)
colnames(citizen_survey) <- c("instanceID", "enumerator", 
                             "schoolid", "school_name", "district", "ward", 
                             "gender", "marital_status", "tribe", "tribe_chewa", "tribe_lomwe", "tribe_ngoni", 
                             "tribe_nyao", "tribe_tumbuka", "tribe_other", "tribe_dk", "tribe_refuse", "tribe_specify", 
                             "age", "ward_length", "education", "income_source", "farming_sell", "business_type", 
                             "children", "children_nr", "children_attend", "teachers_good", "materials_good", 
                             "vote2014", "councillor_done_school", "councillor_done_what", "councillor_done_other_school", "ngo_done", "ngo_done_what", 
                             "councillor_effective", "councillor_work_hard", "councillor_work_hard_specify", "councillor_satisfied", 
                             "councillor_visited", "vote_councillor", "vote_mp", "which_party", "phone_number")



#####SETUP CITIZEN SURVEY DATA#####
citizen_survey$school_home_distance=schools.lc[match(citizen_survey$schoolid, schools.lc$school_id), "school_home_distance"]
citizen_survey$councillor_visited1=ifelse(citizen_survey$councillor_visited<0, NA, citizen_survey$councillor_visited)
citizen_survey$councillor_visited1=ifelse(citizen_survey$councillor_visited==1, 0, citizen_survey$councillor_visited1)
citizen_survey$councillor_visited1=ifelse(citizen_survey$councillor_visited>1, 1, citizen_survey$councillor_visited1)
citizen_survey$councillor_visited=ifelse(citizen_survey$councillor_visited<0, NA, citizen_survey$councillor_visited)

write.csv(citizen_survey, "../data/citizen_survey.csv")
